#!/bin/bash
# Routing configuration example.
# Physical topology:

# +--------------+
# |   baseboxd   |
# |port1    port2|
# +--+--------+--+
#    |        |
#    v        v
#  eth1      eth2
#  ns1       ns2

# On the L3 switch, both ports are configured as gateways
# for the networks on the switch ports. On the servers,
# a manual (or simply a default route) should be inserted,
# to make sure that connectivity exists between the namespaces.

ADDR=("10.20.40.10/24" "10.20.50.10/24")
LOADDR=("10.33.10.10/32" "10.44.10.10/32")

ROUTES=("10.20.50.0/24" "10.20.40.0/24")

GW=("10.20.40.1" "10.20.50.1")

PORTA=${PORTA:-eth1}
PORTB=${PORTB:-eth2}
IFACES=($PORTA $PORTB)

function nsexec {
  ip netns exec ${NS} $@
}

# setup
function setup {

  # generic setup
  for id in {0..1}; do
    export NS=ns$id

    # setting up variables for each namespace
    iface=${IFACES[${id}]}
    ipaddr=${ADDR[${id}]}
    loaddr=${LOADDR[${id}]}
    iproute=${ROUTES[${id}]}
    gw=${GW[${id}]}

    ip netns add ns$id
    ip link set $iface netns ns$id

    ## set links up
    nsexec ip link set $iface up
    nsexec ip address add $ipaddr dev $iface

    ## add route
    nsexec ip route add $iproute via $gw dev $iface
  done
}

setup
